package test230603;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/6/3 20:38
 * 413. 等差数列划分
 * https://leetcode.cn/problems/arithmetic-slices/
 */
public class Solution02 {

    public int numberOfArithmeticSlices(int[] nums) {
        int n = nums.length;
        int ret = 0;  // 记录结果
        // dp[i] 表示以 i 位置元素结尾的子数组中等差数列的个数
        int[] dp = new int[n];
        for (int i = 2; i < n; ++i) {
            dp[i] = nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2] ? dp[i - 1] + 1 : 0;
            ret += dp[i];
        }
        return ret;
    }
}
